//
//  STLogsAddController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
import WMZDialog
import HXPhotoPicker_Lite
class STLogsAddController: STProjectBaseController{
  
    @IBOutlet var picture: UIImageView!
    @IBOutlet var photoView: UIView!
    @IBOutlet var contentView: UIView!
    @IBOutlet var contentTV: UITextView!
    @IBOutlet var dayLabel: UILabel!
    var selectImage:UIImage? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavStyle()
        // Do any additional setup after loading the view.
    }
    func setNavStyle(){
        self.title = "添加日志"
        self.addRightBarButton(imageName: "icon_save")
        self.contentTV.addPlaceholder("开始输入",color: .lightGray)
        let currentDay = Date().toString(format: "yyyy-MM-dd")
        self.dayLabel.text = currentDay + " " + currentDay.dayOfWeekEnglish()!
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.contentView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.photoView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func clockRightBarButtonAction() {
        self.view.endEditing(true)
        if selectImage == nil {
            UIView.showTipsView("请上传状态图片")
            return
        }
        
        if self.contentTV.text!.isEmpty {
            UIView.showTipsView("请输入日志内容")
            return
        }
       
        let model = STLogsModel()
        model.day = Date().toString(format: "yyyy-MM-dd")
        model.log = self.contentTV.text
        model.picture = UIImage.saveImageInCache(self.selectImage!)!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("日志添加成功", true)
        let helper = STLogsModel.getUsingLKDBHelper()
        helper.delete(with: STLogsModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
   
    
    @IBAction func tapAddPictureAction(_ sender: Any) {
        self.view.endEditing(true)
        var config = PickerConfiguration.default
        config.languageType = .simplifiedChinese
        config.maximumSelectedCount = 1
        config.modalPresentationStyle = .fullScreen
        config.selectMode = .single
        config.photoList.allowAddCamera = false
        Photo.picker(
            config
        ) { [self] result, pickerController in
            
            result.getImage(completionHandler: { [self] images in
                picture.image = images.first
                selectImage = images.first
            })
        } cancel: { pickerController in
            // 取消的回调
        }
    }
    
    
    
}
